// packageFT/travel-order-client/apply-customer-service/apply-customer-service.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    content: '',
    order_id: 0,
    api: '',
    active: 0,
    loading: false,
    refundInfo: {}
  },

  /**
   * 提交申请
   */
  submit: function() {
    const { content, order_id, api, active } = this.data
    if (!content) {
      app.tips('请填写申请原因！')
      return
    }
    if (!order_id) {
      app.tips('订单信息有误！')
      return
    }
    if (!api) {
      app.tips('接口地址有误！')
      return
    }
    const urlStr = app.getNetAddresss(api)
    const data = {
      reason: '拍错/多拍/不想要',
      order_id,
      content: content,
      refund_type: 0,
    }
    const _this = this
    this.setData({ loading: true })
    wx.showLoading({ title: '正在提交...'})
    app._postNetWork({
      url: urlStr,
      data,
      success: function(res) {
        const resData = res.data
        if (resData.result == 1) {
          wx.showModal({
            title: '成功',
            content: '已提交，等待工作人员处理！',
            showCancel: false,
            complete: (resC) => {
              if (resC.confirm) {
                const pages = getCurrentPages()
                //获取上一个页面的实例
                const beforePage = pages[pages.length - 2]
                //1、调用上一个页面onLoad
                beforePage.onLoad({ status: active, title: '旅游订单' })
                //2、调用上一个页面刷新方法
                beforePage.getOrders()
                wx.navigateBack({ delta: -1 })
              }
            }
          })
        } else {
          app.tips(resData.msg || '提交失败！')
        }
      },
      complete: function() {
        wx.hideLoading()
        _this.setData({ loading: false })
      }
    })
  },

  /**
   * 获取退款信息
   */
  getRefundInfo: function() {
    const { order_id } = this.data
    const urlStr = app.getNetAddresss('plugin.guide.frontend.need-order-refund.params')
    const _this = this
    wx.showLoading({ title: '数据加载中...' })
    app._getNetWork({
      url: urlStr,
      data: { order_id },
      success: function(res) {
        const resData = res.data
        if (resData.result == 1) {
          _this.setData({ refundInfo: resData.data })
        } else {
          wx.showModal({
            title: '提示',
            content: resData.msg || '退款信息获取失败！',
            showCancel: false,
            complete: (resC) => {
              if (resC.confirm) {
                wx.navigateBack({ delta: -1 })
              }
            }
          })
        }
      },
      complete: function() {
        wx.hideLoading()
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      order_id: options?.order_id || 0,
      api: options?.api || '',
      active: options?.active || 0
    })
    this.getRefundInfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})